/*
 * @(#)AtnaClientProducer.java
 * Date 2013-11-05
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.atna.client.inject;

import javax.enterprise.context.ApplicationScoped;

import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;

import javax.inject.Inject;

import us.hornerscorners.lamppost.atna.client.AtnaContext;
import us.hornerscorners.lamppost.atna.client.AtnaPooledSslTcpClient;
import us.hornerscorners.lamppost.atna.client.AtnaPooledTcpClient;
import us.hornerscorners.lamppost.atna.client.AtnaUdpClient;
import us.hornerscorners.lamppost.atna.client.IAtnaClient;

import us.hornerscorners.lamppost.ihecommon.config.AtnaConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-11-05
 * @author         Jim Horner
 */
public class AtnaClientProducer {

    /** Field description */
    @Inject
    private AtnaContext atnaContext;

    /** Field description */
    @Inject
    private IHEConfig config;

    /**
     * Method description
     *
     *
     * @param atnaContext
     * @param atnaConfig
     *
     * @return
     */
    public static IAtnaClient produceClient(AtnaContext atnaContext,
            AtnaConfig atnaConfig) {

        IAtnaClient result = null;

        switch (atnaConfig.getProtocol()) {

            case ssltcp :
                result = new AtnaPooledSslTcpClient(atnaContext, atnaConfig);

                break;

            case tcp :
                result = new AtnaPooledTcpClient(atnaContext, atnaConfig);

                break;

            case udp :
                result = new AtnaUdpClient(atnaContext, atnaConfig);

                break;

            default :
                throw new IllegalArgumentException(
                    "Unrecognized ATNA protocol in config.");
        }

        if (result != null) {

            result.startup();
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param atnaClient
     */
    public void disposes(@Disposes IAtnaClient atnaClient) {

        atnaClient.shutdown();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Produces
    @ApplicationScoped
    public IAtnaClient produces() {

        return produceClient(this.atnaContext, this.config.getAtnaConfig());
    }
}
